import argparse
import time

from libs import config
from libs.scheduler import Scheduler
from libs.scheduler_v2 import SchedulerV2, TaskStatus
from libs.window import AdsWindow


class TaskHelper:
    def __init__(self, user_id):
        self.scheduler = SchedulerV2()
        self.user_id = user_id
        self.task_id = None
        self.retry = 0

    @staticmethod
    def default_args_parser(description):
        # 解析参数
        parser = argparse.ArgumentParser(description=description)
        parser.add_argument('--user_id', type=int, required=True, help="用户ID")
        parser.add_argument('--task_id', type=int, help="任务ID")
        parser.add_argument('--retry', type=int, help="重试次数")
        return parser

    @staticmethod
    def from_args(args):
        task_helper = TaskHelper(args.user_id)
        task_helper.task_id = args.task_id
        if args.retry:
            task_helper.retry = args.retry
        return task_helper

    def farm(self, soil_id, farm_item, delay_seconds=0, ignore_running=True, retry=False):
        args = {"soil_id": str(soil_id), "farm_item": str(farm_item)}
        self.add_task('farm.py', delay_seconds, ignore_running, retry, args=args)

    def charge(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('charge.py', delay_seconds, ignore_running, retry)

    def trade(self, delay_seconds=0, ignore_running=True, ignore_pending=False, priority=None, retry=False):
        script = 'trade_v3.py'
        if ignore_pending:
            self.scheduler.delete_pending_task(script, self.user_id)
        self.add_task(script, delay_seconds, ignore_running, retry, priority=priority)

    def saona(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('saona.py', delay_seconds, ignore_running, retry)

    def cooking_pop(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('cooking_pop.py', delay_seconds, ignore_running, retry)

    def cooking_grumpkin(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('cooking_grumpkin.py', delay_seconds, ignore_running, retry)

    def windmill(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('windmill.py', delay_seconds, ignore_running, retry)

    def wine(self, delay_seconds=0, ignore_running=True, priority=None, retry=False):
        self.add_task('wine.py', delay_seconds, ignore_running, retry, priority=priority)

    def wood(self, delay_seconds=0, ignore_running=True, priority=None, retry=False):
        self.add_task('wood.py', delay_seconds, ignore_running, retry, priority=priority)

    def stone(self, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task('stone.py', delay_seconds, ignore_running, retry)

    def silk_slug(self, land_id, delay_seconds=0, ignore_running=True, retry=False):
        self.add_task(f'silk_slug_{land_id}.py', delay_seconds, ignore_running, retry)

    def add_task(self, script, delay_seconds, ignore_running=False, retry=False, args=None, priority=None):
        if args is None:
            args = {}
        if priority is None:
            if script == 'saona.py':
                priority = 0
            elif 'silk_slug' in script:
                priority = 2
            elif script == 'trade_v3.py':
                priority = 3
            elif script == 'farm.py':
                priority = 3
            elif script == 'forest_and_wood.py':
                priority = 4
            elif script == 'collect.py':
                priority = 10
            else:
                priority = 1
        if retry:
            if self.retry == 0:
                delay_seconds = max(10, delay_seconds)  # 第一次10秒后重试
            elif self.retry == 1:
                delay_seconds = max(60 * 10, delay_seconds)  # 第二次10分钟后重试
            elif self.retry == 2:
                delay_seconds = max(60 * 60 * 3, delay_seconds)  # 第三次3小时后重试
            else:
                return
            priority += 1
            args['retry'] = self.retry + 1
            print(self.retry, args)

        self.scheduler.add_task(
            script,
            self.user_id,
            args=args,
            priority=priority,
            delay_seconds=delay_seconds,
            ignore_running=ignore_running
        )

    # 任务执行成功
    def task_success(self):
        if self.task_id is not None:
            self.scheduler.finish(self.task_id, TaskStatus.DONE)

    # 任务执行失败
    def task_fail(self):
        if self.task_id is not None:
            self.scheduler.finish(self.task_id, TaskStatus.FAIL)

    def task_giveup(self):
        if self.task_id is not None:
            self.scheduler.giveup(self.task_id)